package com.xuan.bkm.service.user.admin.config;

import com.xuan.bkm.constants.SystemConstants;
import com.xuan.bkm.service.user.admin.dao.IAdminDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Configuration
@Slf4j
public class UserRedisConfig {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IAdminDao iad;

    /**
     * 缓存预热
     * @return
     */

    @Bean
    public InitializingBean cachePreLoadData(){
        return new InitializingBean() {
            @Override
            public void afterPropertiesSet() throws Exception {
                Set<String> allNums=iad.findAllNums();
                stringRedisTemplate.opsForSet()
                        .add(SystemConstants.REDIS_ADMIN_NUMS,allNums.toArray(new String[allNums.size()]));
                stringRedisTemplate.expire(SystemConstants.REDIS_ADMIN_NUMS,3, TimeUnit.HOURS);

                log.info("redis缓存预热成功，读取到所有的admin用户");
            }
        };
    }
}
